package com.shinhye.mobilelottery.ui;

import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shinhye.mobilelottery.ConstantValue;
import com.shinhye.mobilelottery.GlobalParams;
import com.shinhye.mobilelottery.R;
import com.shinhye.mobilelottery.engine.CommonInfoEngine;
import com.shinhye.mobilelottery.net.protocol.Element;
import com.shinhye.mobilelottery.net.protocol.Message;
import com.shinhye.mobilelottery.net.protocol.Oelement;
import com.shinhye.mobilelottery.net.protocol.element.IssueInfoElement;
import com.shinhye.mobilelottery.ui.manager.MiddleManager;
import com.shinhye.mobilelottery.utils.BeanFactory;
import com.shinhye.mobilelottery.utils.PromptManager;
import com.shinhye.mobilelottery.utils.TimeUtils;

public class HallUI extends BaseUI{
	private int[] logoResIds;
	private int[] titleResIds;
	private ListView lv_content;
	private ViewPager viewpager;
	private ArrayList<TextView> tv_list;
	private ArrayList<View> pagerList;
	private ImageView underline;
	private int currPage = 0;
	private TextView tv_fc;
	private Bundle bundle;
	
	public HallUI(Context context) {
		super(context);
	}
	
	@Override
	protected void onCreate() {
		View showView = View.inflate(getContext(), R.layout.il_hall, null);
		viewpager = (ViewPager) showView.findViewById(R.id.ii_vp);
		underline = (ImageView) showView.findViewById(R.id.ii_category_selector);
		tv_fc = (TextView) showView.findViewById(R.id.ii_category_fc);
		TextView tv_tc = (TextView) showView.findViewById(R.id.ii_category_tc);
		TextView tv_gpc = (TextView) showView.findViewById(R.id.ii_category_gpc);
		
		tv_list = new ArrayList<TextView>();
		tv_list.add(tv_fc);
		tv_list.add(tv_tc);
		tv_list.add(tv_gpc);
		
		logoResIds = new int[] {R.drawable.id_ssq, R.drawable.id_3d, R.drawable.id_qlc};
		titleResIds = new int[] {R.string.is_hall_ssq_title, R.string.is_hall_3d_title, R.string.is_hall_qlc_title};
		
		lv_content = new ListView(getContext());
		MyBaseAdapter lvAdapter = new MyBaseAdapter();
		lv_content.setAdapter(lvAdapter);
		
		initUnderLine();
		initPager();
		MyPagerAdapter vpAdapter = new MyPagerAdapter();
		viewpager.setAdapter(vpAdapter);
		
		setContentView(showView);
	}
	
	private void initUnderLine() {
		Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.id_category_selector);
		Matrix matrix = new Matrix();
		int offset = (GlobalParams.windowWidth / 3 - bitmap.getWidth()) / 2;
		matrix.postTranslate(offset, 0);
		underline.setImageMatrix(matrix);
	}

	private void initPager() {
		pagerList = new ArrayList<View>();
		pagerList.add(lv_content);
		
		TextView tc = new TextView(getContext());
		tc.setText("体彩");
		tc.setTextSize(24);
		tc.setTextColor(Color.MAGENTA);
		pagerList.add(tc);
		
		TextView gpc = new TextView(getContext());
		gpc.setText("高频彩");
		gpc.setTextSize(24);
		gpc.setTextColor(Color.YELLOW);
		pagerList.add(gpc);
	}

	class MyPagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return pagerList.size();
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = pagerList.get(position);
			container.addView(view);
			return view;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			View view = pagerList.get(position);
			container.removeView(view);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
	}
	
	class MyBaseAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return logoResIds.length;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			View view;
			ViewHolder holder;
			if(convertView != null) {
				view = convertView;
				holder = (ViewHolder) view.getTag();
			} else {
				view = View.inflate(getContext(), R.layout.il_hall_lottery_item, null);
				holder = new ViewHolder();
				holder.logo = (ImageView) view.findViewById(R.id.ii_hall_lottery_logo);
				holder.title = (TextView) view.findViewById(R.id.ii_hall_lottery_title);
				holder.summary = (TextView) view.findViewById(R.id.ii_hall_lottery_summary);
				holder.bet = (ImageView) view.findViewById(R.id.ii_hall_lottery_bet);
				view.setTag(holder);
			}
			
			holder.logo.setImageResource(logoResIds[position]);
			holder.title.setText(titleResIds[position]);
			holder.bet.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(position == 0)
					{
						// 跳转到双色球选号界面
						MiddleManager.getInstance().changeUI(PlaySSQUI.class, bundle);
					}
				}
			});
			holder.summary.setTag(position);
			
			return view;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
	}
	
	static class ViewHolder {
		ImageView logo;
		TextView title;
		TextView summary;
		ImageView bet;
	}
	
	private void getIssueInfo() {
		new MyAsyncTask<Integer>() {
			@Override
			protected Message doInBackground(Integer... params) {
				int id = params[0];
				CommonInfoEngine infoEngine = BeanFactory.getImpl(CommonInfoEngine.class);
				Message res = infoEngine.getIssuesInfo(id);
				return res;
			}

			@Override
			protected void onPostExecute(Message result) {
				if(result == null)
					return;
				
				Oelement oelement = result.getBody().getOelement();
				if(ConstantValue.SUCCESS.equals(oelement.getErrorcode())) {
					changeNotice(result.getBody().getElements().get(0));
				} else {
					PromptManager.showErrorDialog(getContext(), oelement.getErrormsg());
				}
			}
		}.execute(ConstantValue.SSQ);
	}
	
	private void changeNotice(Element element) {
		IssueInfoElement infoEle = (IssueInfoElement) element;
		String issue = infoEle.getIssue().getValue();
		String lasttime = TimeUtils.getStandardTime(infoEle.getLasttime().getValue());
		String text = getContext().getResources().getString(R.string.is_hall_common_summary);
		text = StringUtils.replaceEach(text, new String[] { "ISSUE", "TIME" }, new String[] { issue, lasttime });
		
		TextView textView = (TextView) lv_content.findViewWithTag(0);
		if (textView != null) {
			textView.setText(text);
		}
		
		bundle = new Bundle();
		bundle.putString("issue", issue);
	}
	
	@Override
	public int getId() {
		return ConstantValue.VIEW_HALL;
	}

	@Override
	protected void setListener() {
		viewpager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				TranslateAnimation anim = new TranslateAnimation(currPage * GlobalParams.windowWidth / 3, position * GlobalParams.windowWidth / 3, 0, 0);
				anim.setDuration(300);
				anim.setFillAfter(true);
				underline.startAnimation(anim);
				
				tv_list.get(currPage).setTextColor(Color.BLACK);
				tv_list.get(position).setTextColor(Color.RED);
				
				currPage = position;
			}
			
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {}
			@Override
			public void onPageScrollStateChanged(int state) {}
		});
		
		tv_fc.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				viewpager.setCurrentItem(0, true);
			}
		});
	}

	@Override
	public void onResume() {
		getIssueInfo();
	}

}
